#! /bin/bash

## SCRIPT   : baiddump3.sh
## AUTHOR   : huanlf@gmail.com
## VERSION  : 0.1
## LICENCES : GPL v2
## LAST MOD : 2008-05-18 17:29

URL_TOP100='http://list.mp3.baidu.com/list/newhits.html?id=1?top1'
URL_TOP500='http://list.mp3.baidu.com/topso/mp3topsong.html?id=1?top2'

TMPDIR=/tmp/bdmp3.$$
DESTDIR=~/music/baidu

WGET="wget --timeout 30 --tries=1 --limit-rate=50k"
BLACKLIST=~/.myblack_list ## do not download files in blacklist 
RM="/bin/rm -f"

CUR_CNT=0

## reutrn codes
SUPPORTED="mp3 wma ogg"
DOWNLOAD_OK=0
DOWNLOAD_FAIL=1
FILE_EXIST=2
FILE_IN_BLACK=3
FILE_NOT_SUPPORT=4

RED=$(    echo -e "\e[31;40m" )
GREEN=$(  echo -e "\e[32;40m" )
YELLO=$(  echo -e "\e[33;40m" )
BLUE=$(   echo -e "\e[34;40m" )
MAGENTA=$(echo -e "\e[35;40m" )
CYAN=$(   echo -e "\e[36;40m" )
RESET=$(  echo -e "\e[0m"     )

## colorful echo
msg()  { echo "${GREEN}$*${RESET}";   }
rmsg() { echo "${RED}$*${RESET}";     }
gmsg() { echo "${GREEN}$*${RESET}";   }
ymsg() { echo "${YELLO}$*${RESET}";   }
bmsg() { echo "${BLUE}$*${RESET}";    }
mmsg() { echo "${MAGENTA}$*${RESET}"; }
cmsg() { echo "${CYAN}$*${RESET}";    }

usage()
{
    {
        echo "Usage:    $(basename $0) [100|500]"
        echo "100:      baidu top100 mp3"
        echo "500:      baidu top500 mp3"
        echo "default:  500"
    } >&2
}

check_music()
{
    local f=$1

    if file "$f" | awk -F: '{ print $2 }' |
        grep -Eq 'asf|mp3|mpeg|audio'; then
        return 0
    else
        return 1
    fi
}

mkdir -p $TMPDIR
mkdir -p $DESTDIR 

cd $TMPDIR && rm -rf -- *
touch "$BLACKLIST"

download()
{
    local file=$1
    local line href postfix finallink

    while read line; do
        eval "$line" ## href=xxx 
        name=$( echo $line | grep -oEm1 '\[[^]]+\]' ) ## [%C9%E1%B2%BB%B5%C3+%CF%D2%D7%D3]
        ## echo "$name"
        filename=$( url_decode "$name" | sed -e 's/\[//g' -e 's/\]//' -e  's/\+/_/'  )
        ## echo [$filename]

        $RM 3.html
        $WGET -O- "$href" | iconv -f gbk -t utf8 > 3.html | continue
        [[ -f 3.html ]] || continue

        ## 3.html: href="http://opp.iuuiu.cn/IU/abcah/fdjacj.mp3"
        finallink=$( grep -oEm1 'href="http://.*?"' 3.html )
        #echo "final link [$finallink]"
        eval "$finallink"    ## href="xxx"

        local t
        for t in $SUPPORTED; do
            postfix=$( echo $href | grep -Eio "\.${t}$" ) && break
        done

        if [[ -z "$postfix" ]]; then
            rmsg "NO. $CUR_CNT: NOT SUPPORTED [${href##*.}] YET ..."
            continue
            ##return $FILE_NOT_SUPPORT
        fi

        if grep -wqF "$filename" "$BLACKLIST"; then
            mmsg "NO. $CUR_CNT: [$filename] IN BLACKLIST, IGNORED ..." 
            return $FILE_IN_BLACK
        fi

        finalname="${filename}${postfix}"

        for t in $SUPPORTED; do
            if ls "/$DESTDIR/$filename.$t" &>/dev/null; then
                ymsg "NO. $CUR_CNT: ALREADY DOWNLOADED [$filename.$t]. IGNORED ..."
                return $FILE_EXIST
            fi
        done

        mmsg "NO. $CUR_CNT: DOWNLOADING [$finalname] VIA [$href] ..."
        if $WGET -O "$finalname" "$href"; then
            gmsg "SUCCESSFULLY DOWNLOADED [$finalname]" 

            ## make sure we download a song
            if ! check_music "$finalname"; then
                rmsg "CHECK FILE TYPE FAILED, TRY TO DOWNLOAD AGAIN ..."
                continue
            fi

            mv "$finalname" "$DESTDIR/"
            return $DOWNLOAD_OK 
        else
            rmsg "DOWNLOAD FAILED [$finalname]" 
            $RM "$finalname"
        fi

    done < $file

    return $DOWNLOAD_FAIL
}

## ---------------- main ---------------- ##

if ! which url_decode.pl &>/dev/null; then
    rmsg "url_decode.pl NOT FOUND, AUTO AUTO DOWNLOADING ..."
    mkdir -p ~/bin
    if ! wget -O ~/bin/url_decode.pl http://huan.googlecode.com/svn/perl/url_decode.pl; then
        rmsg "DOWNLOAD [url_decode.pl] FAILED, ABORTING ..."
    fi

    chmod +x ~/bin/url_decode.pl
    export PATH=$PATH:~/bin
fi


## accept positional argv: 100 | 500
## or may called by name: *100.sh | *500.sh 
arg=$1

if [[ -n $arg ]]; then
    case $arg in
    -h|--usage|--help)
       usage
       exit 1
       ;;
    100)
        URL=$URL_TOP100
        ;;
    *)
        URL=$URL_TOP500
        ;;
    esac
else
    case $( basename $0 ) in
        *100.sh)
            URL=$URL_TOP100
            ;;
        *)
            URL=$URL_TOP500
            ;;
    esac
fi
    
## clean tmpdir before exit
trap "/bin/rm -rf $TMPDIR; exit" 2 3 1

gmsg "START TO DOWNLOAD SONGS FROM mp3.baidu.com ..."

$RM 1.html
$WGET -O- "$URL" | iconv -f gbk -t utf8 |
    grep -h -o 'href="http://mp3.baidu.com/m?tn=baidump3&ct=.*</a>$' | 
    awk '{ print $1 }' > 1.html

if [[ ! -s 1.html ]]; then
    rmsg "CAN NOT GET LINKS VIA [$URL] ..." >&2
    exit 255
fi

## 1.html:  href="http://mp3.baidu.com/m?tn=baidump3&ct=134217728&lm=-1&li=2&word=%CF%E0%B0%AE%B5%C4%B8%E8+%B0%A2%B6%E4" 

while read line; do
    eval "$line"  ## href="xxx"   
    keyword=$( echo $line | grep -oE 'ct=[0-9]+' )
    ## echo "[$keyword]"
    $WGET -O- $href | iconv -f gbk -t utf8 |
        grep -oE --color "href=\"http://.*${keywork}&tn=baidusg.*?\"[[:space:]]*title" |
        awk -F'title' '{ print $1 }' > 2.html

    (( CUR_CNT++ ))
    download 2.html

    $RM 2.html

    #exit
done < 1.html

gmsg "ALL DONE"
$RM -rf "$TMPDIR"
